package Logic;

import microgame.airplane.proto.Game;
import microgame.airplane.proto.MsgTypeOuterClass;

import java.util.Hashtable;

public class PropManager {
    private Match match;
    private Hashtable<Integer, PropGroup> propGroupMap = new Hashtable<>();

    public PropManager(Match match) {
        this.match = match;
    }

    public Match GetMatch () {
        return match;
    }

    public Prop CreateProp(int playerId, int templateId, float x, float y, float angle) {
        Prop prop = new Prop(playerId, templateId, x, y, angle);
        if (propGroupMap.get(templateId) == null) {
            propGroupMap.put(templateId, new PropGroup());
        }
        propGroupMap.get(templateId).AddProp(prop);

        SyncAllClient(prop);

        return prop;
    }

    public void SyncAllClient(Prop prop) {
        //发送生成道具的消息
        Game.sPropSpawnPush.Builder builder = Game.sPropSpawnPush.newBuilder();
        builder.setTemplateid(prop.templateId);
        builder.setPlayerid(prop.playerId);
        builder.setId(prop.Id);

        Game.sPropSpawnPush push = builder.build();

        match.SendMessageToAllClients(MsgTypeOuterClass.MsgType.sPropSpawnPush.getNumber(), push.toByteArray());
    }
}
